package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.crawler.job.AllProduct;
import com.xxl.job.executor.crawler.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * 描述:
 *
 * @author Pfly
 * @create 2019-01-05 9:08
 */
@JobHandler(value = "CrawlerHandler")
@Component
public class CrawlerProduct extends IJobHandler {
    @Autowired
    AllProduct allProduct;

    @Override
    public ReturnT<String> execute(String param) {
        Jedis jedis = JedisUtil.getJedis();
        XxlJobLogger.log("启动爬虫！");
        System.out.println("启动爬虫");

        //此方法需要递归
        allProduct.insert(param);

        if (jedis.lrange("productUrl", 0, 1).size() == 0) {
            return SUCCESS;
        } else if(Integer.valueOf(param) == 0) {
            return SUCCESS;
        }

        XxlJobLogger.log("爬虫结束！");
        System.out.println("爬虫结束");

        return execute(String.valueOf(Integer.valueOf(param)-1));
    }
}
